package com.shuzi.MediaShare.view;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * @类描述：自动换行布局
 * @作者：lung
 * @日期： 2013-10-24 下午3:32:41
 *
 */
public class AutoLineFeed extends ViewGroup
{

    private int horizontalSpace = 15;

    private int verticalSpace = 8;

    private Map<String, Position> map;

    private int right;

    public AutoLineFeed(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    public AutoLineFeed(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public AutoLineFeed(Context context)
    {
        super(context);
        init(context);
    }

    private void init(Context context)
    {
        horizontalSpace = dip2px(context, horizontalSpace);
        verticalSpace = dip2px(context, verticalSpace);
        map = new HashMap<String, AutoLineFeed.Position>();
        right = getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int mTotalHeight = 0;
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY)
        {
            right = widthSize;
        }

        int count = getChildCount();

//        if (count == 0)
//        {
//            return;
//        }

        int row = 0;// which row lay you view relative to parent
        int lengthX = 0; // right position of child relative to parent
        int lengthY = 0; // bottom position of child relative to parent
        boolean rowFirst = true;
        boolean columnFirst = true;

        for (int index = 0; index < count; index++)
        {

            final View child = getChildAt(index);
            // measure
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

            Position position = new Position();
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            if (columnFirst)
            {
                lengthY = height;
            }
            else
            {
                lengthY = row * (height + verticalSpace) + height;
            }
            if (rowFirst)
            {
                lengthX += width;
                rowFirst = false;
            }
            else
            {
                lengthX += width + horizontalSpace;
            }

            // 换行
            if (lengthX > right)
            {
                lengthX = width;
                row++;
                lengthY = row * (height + verticalSpace) + height;
                columnFirst = false;

            }
            position.left = lengthX - width;
            position.right = lengthX;
            position.top = lengthY - height;
            position.bottom = lengthY;
            mTotalHeight = position.bottom;

            // Log.d("lung", "position = " + index + " left = " + position.left
            // + " top = " + position.top + " right = " + position.right
            // + " botom = " + position.bottom);
            map.put(index + "", position);
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int measuredHeight = heightSize;
        if (heightMode != MeasureSpec.EXACTLY)
        {
            measuredHeight = mTotalHeight;
            Log.d("lung", "MeasureSpec.EXACTLY");
        }
        setMeasuredDimension(widthSize, measuredHeight);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        Log.d("lung", "changed = " + changed + " left = " + l + " top = " + t
                + " right = " + r + " botom = " + b);
        final int count = getChildCount();
        for (int i = 0; i < count; i++)
        {
            View child = getChildAt(i);
            Position pos = map.get(i + "");
            if (pos != null)
            {
                child.layout(pos.left, pos.top, pos.right, pos.bottom);
            }
            else
            {
                Log.i("MyLayout", "error");
            }
        }

    }

    private class Position
    {
        int left, top, right, bottom;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


}